//
//  SiteMapViewController.m
//  AirConditioner
//
//  Created by Sean Wang on 12-5-6.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SiteMapViewController.h"
#import "BrandBottomViewController.h"

@interface SiteMapViewController ()

@end

@implementation SiteMapViewController

@synthesize coordinate, siteName;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Result";
        
    }
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = 37.786996;
    theCoordinate.longitude = -122.419281;
    self.coordinate = theCoordinate;
    
    //NSLog(@"initWithNibName,%f",self.coordinate.latitude);
    //NSLog(@"initWithNibName,%f",self.coordinate.longitude);
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //[self performSelector:@selector(setMapCenter) withObject:nil afterDelay:1];
    UIViewController *viewController = [[BrandBottomViewController alloc] initWithNibName:@"BrandBottomView" bundle:nil];
    [self.view addSubview:viewController.view];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    tel = @"0398071165";//will get this kind of information from REST
    mob = @"0418993668";//will get this kind of information from REST
    
    [self setMapCenter];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [mapView removeAnnotation:pAnnotation];
}

- (void)setMapCenter
{
    MKCoordinateRegion newRegion;
    //newRegion.center = self.coordinate;
    //NSLog(@"%f",self.coordinate.latitude);
    //NSLog(@"%f",self.coordinate.longitude);
    newRegion.center.latitude = self.coordinate.latitude;
    newRegion.center.longitude = self.coordinate.longitude;
    newRegion.span.latitudeDelta = 0.02;
    newRegion.span.longitudeDelta = 0.02;
    
    [mapView setRegion:[mapView regionThatFits:newRegion] animated:YES];    
    pAnnotation = [[MKPointAnnotation alloc] init];
    
    pAnnotation.coordinate = newRegion.center;
    pAnnotation.title = self.siteName;
    
    [mapView addAnnotation:pAnnotation];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)callOut:(id)sender {
    UIButton *button = (UIButton*)sender;
    NSString *callNumber;
    switch (button.tag) {
        case 1:
            callNumber = tel;
            break;
        case 2:
            callNumber = mob;
            break;
        default:
            break;
    }
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", callNumber]]];
}

@end
